﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.BBS;
using DFGK.IService.BBS;
using DFGK_WMS.Context;

namespace DFGK.Service.BBS
{
    public class EquipmentBadService : BaseService, IEquipmentBadService
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly IEquipmentBadRepository _equiBadRepo;
        private readonly DFGK_DB _DBContext;
        public EquipmentBadService(IMapper mapper, IUnitOfWork unitOfWork, IEquipmentBadRepository equiBadRepo, DFGK_DB dBContext = null) : base(mapper)
        {
            _unitOfWork = unitOfWork;
            _equiBadRepo = equiBadRepo;
            _DBContext = dBContext;
        }

        public List<EquipmentBad> GetPageData(string? materialName, int pageIndex, int pageSize, out int totalCount)
        {
            var query = _equiBadRepo.GetEntityAll();
            if (!string.IsNullOrEmpty(materialName))
            {
                query = query.Where(t => t.MaterialName.Contains(materialName));
            }

            totalCount = query.Count();
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(o => o.Id);
            return query.ToList();
        }

        public bool SubmitEquipmentBad(EquipmentBad equipmentBad, out string msg)
        {
            msg = "";
            if (equipmentBad.Id > 0)
            {
                _equiBadRepo.UpdEntity(equipmentBad);
            }
            else
            {
                _equiBadRepo.AddEntity(equipmentBad);
            }
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelEquimentBad(long postId, out string msg)
        {
            msg = "";
            var post = _equiBadRepo.QueryEntityById(postId);
            if (post == null)
            {
                msg = "参数错误";
                return false;
            }
            _equiBadRepo.DelEntity(post);
            return _unitOfWork.SaveChangesEntity();
        }
        public EquipmentBad? GetEquipmentBad(long equipmentBadId)
        {
            return _equiBadRepo.QueryEntityById(equipmentBadId);
        }

        public object EquipmentBadTongJi()
        {
            return _equiBadRepo.GetEntityAll().GroupBy(t => new { t.MaterialCode, t.MaterialName }).Select(t => new
            {
                t.Key.MaterialCode,
                t.Key.MaterialName,
                Count = t.Sum(p => p.MaterialId)
            }).ToList();
        }


        public bool EquipmentBadSP(long equipmentBadId, string status, out string errorMsg)
        {
            errorMsg = "";
            if (string.IsNullOrEmpty(status))
            {
                errorMsg = "审批意见不能为空";
                return false;
            }
            var equiBad = _equiBadRepo.QueryEntityById(equipmentBadId);
            if (equiBad == null)
            {
                errorMsg = "参数错误";
                return false;
            }
            equiBad.Status = status;
            _equiBadRepo.AddEntity(equiBad);
            _unitOfWork.SaveChangesEntity();
            return true;


        }

        public List<EquipmentBad> GetEquipmentBadDown()
        {
            var list = _equiBadRepo.GetAllEntityList();
            return list;
        }

    }
}
